<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.push({ path: '/home' })"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          :src="article.aut_photo"
          round
          width="1rem"
          height="1rem"
          fit="fill"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | fromNow }}</p>
        </div>
        <van-button round size="small" type="info" @click="toggleFollow">
          {{ article.is_followed === true ? '已关注' : '关注' }}
        </van-button>
      </div>
      <div class="content">
        <div v-html="article.content">正文</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          @click="toggleLike"
          >{{ article.attitude === -1 ? '已赞' : '点赞' }}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="toggleDislike"
          >{{
            article.is_collected === true ? '已不喜欢' : '不喜欢'
          }}</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->
  </div>
</template>

<script>
import {
  getArticle,
  followUser,
  unFollowUser,
  likings,
  unLikings,
  dislike,
  unDislike
} from '../../api/article'
import { getToken } from '../../utils/token'
export default {
  name: 'Article',
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {}
    }
  },
  methods: {
    async toggleLike () {
      // token 的非空判断
      if (getToken() === null) {
        // 没登陆
        this.$toast.fail('请先登录')
        // 去登陆
        this.$router.push({ path: '/login', query: { back: this.$route.path } })
        return
      }
      // 判断点赞状态
      if (this.article.attitude === -1) {
        // 点赞
        const res = await unLikings({ target: this.article.aut_id })
        this.$toast.success('已取消赞！')
        this.article.attitude = 0
        console.log(res)
      } else {
        // 取消点赞
        const res = await likings({ target: this.article.aut_id })
        this.$toast.success('点赞成功！')
        this.article.attitude = -1
        console.log(res)
      }
    },
    async toggleDislike () {
      const res = await unDislike({ target: this.article.aut_id })
      console.log(res)
      // token 的非空判断
      if (getToken() === 1) {
        // 没登陆
        this.$toast.fail('请先登录')
        // 去登陆
        this.$router.push({ path: '/login', query: { back: this.$route.path } })
        return
      }
      // 判断关注状态

      if (this.article.is_collected === true) {
        // 喜欢文章
        const res = await unDislike({ target: this.article.aut_id })
        this.$toast.success('取消文章不喜欢')
        this.article.is_collected = false
        console.log(res)
      } else {
        // 不喜欢文章
        const res = await dislike({ target: this.article.aut_id })
        this.$toast.success('对文章不感兴趣！')
        this.article.is_collected = true
        console.log(res)
      }
    },
    async toggleFollow () {
      // token 的非空判断

      if (getToken() === null) {
        // 没登陆
        this.$toast.fail('请先登录')
        // 去登陆
        this.$router.push({ path: '/login', query: { back: this.$route.path } })
        return
      }
      // 判断关注状态
      if (this.article.is_followed === true) {
        // 取关
        const res = await unFollowUser({ target: this.article.aut_id })
        this.$toast.success('取关成功！')
        this.article.is_followed = false
        console.log(res)
      } else {
        // 关注
        const res = await followUser({ target: this.article.aut_id })
        this.$toast.success('关注成功！')
        this.article.is_followed = true
        console.log(res)
      }
    }
  },
  async created () {
    const res = await getArticle({ id: this.$route.params.id })
    console.log('res:', res)
    this.article = res.data.data
    this.loading = false
  }
}
</script>
<style lang="less">
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  .article-loading {
    padding-top: 100px;
    text-align: center;
  }
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
  }
  .error {
    padding-top: 100px;
    text-align: center;
  }
  .detail {
    padding: 50px 10px;
    .title {
      font-size: 16px;
    }
    .zan {
      text-align: center;
    }
    .author {
      padding: 10px 0;
      display: flex;
      .text {
        flex: 1;
        padding-left: 10px;
        line-height: 1.3;
        .name {
          font-size: 14px;
          margin: 0;
        }
        .time {
          margin: 0;
          font-size: 12px;
          color: #999;
        }
      }
    }
    .content {
      font-size: 14px;
      overflow: hidden;
      white-space: pre-wrap;
      word-break: break-all;
      ::v-deep img {
        max-width: 100%;
        background: #f9f9f9;
      }
    }
  }
}
</style>
